#!/usr/bin python
from kafka import KafkaConsumer
from google.protobuf.json_format import MessageToJson, Parse
import json
# connect to Kafka server and pass the topic we want to consume
from ai import genericobj_pb2

consumer = KafkaConsumer('face-topic',group_id = 'test_group', bootstrap_servers='10.20.5.3:9092')
# consumer.subscribe('face-topic')

obj = genericobj_pb2.GenericObj()

vehobj = genericobj_pb2.VehicleObj()
pedobj =  genericobj_pb2.PedestrianObj()
nonobj =  genericobj_pb2.NonMotorVehicleObj()
faceobj = genericobj_pb2.FaceObj()


def decode():
	try:
		for msg in consumer:
			#print(msg)
			#print("%d:%d:%d,value=%s" % (msg.partition, msg.offset, len(msg.value), msg.value))
			obj.ParseFromString(msg.value)
			#print obj
			print ('------------------')
			#print obj.ObjType
			#print type(obj.ObjType)
			if obj.ObjType == 1:
				vehobj.ParseFromString(obj.BinData)
				#print vehobj
				#json = MessageToJson(vehobj)
				#print json
				#break
			elif obj.ObjType == 2 or obj.ObjType == 3:
				nonobj.ParseFromString(obj.BinData)
                                #json = MessageToJson(nonobj)
                                #print json
				#print nonobj
				#break
			elif obj.ObjType == 4:
				pedobj.ParseFromString(obj.BinData)
				print (str(json.loads(MessageToJson(pedobj))))
				json.dumps(json.loads(MessageToJson(pedobj), encoding='utf-8'),encoding='utf-8')
                                #print type(pedobj.Pedestrian)
                                #json = MessageToJson(pedobj)
                                #print json
				#break
			elif obj.ObjType == 1024:
				faceobj.ParseFromString(obj.BinData)
                                #json = MessageToJson(faceobj)
                                #print json
				#print faceobj
				#break
	except KeyboardInterrupt as e:
		print(e)


if __name__ == '__main__':
	decode()
